<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "@/views/dashboard/mixins/resize.js";

export default {
  mixins: [resize],
  props: {
    xAxis: {
      type: Array,
      default: [],
    },
    yAxis: {
      type: Array,
      default: [],
    },
    title: {
      type: Object,
      default: {},
    },
    toolbox: {
      type: Object,
      default: {},
    },
    quotaIds: {
      type: Array,
      default: []
    },
    legend: {
      type: Array,
      default: [],
    },
    data: {
      type: Array,
      default: [],
    },
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
      series:[]
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
    
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions();
    },
    setOptions() {
      this.quotaIds.forEach((item,index) => {
      this.series.push({
        name: this.legend[index],
        data: this.data[this.quotaIds[index]],
        type: "line",
      })
    })
      this.chart.setOption({
        title: this.title,
        grid:{
          left:"240px"
        },
        toolbox: this.toolbox,
        legend: {
          data: this.legend,
        },
        xAxis: this.xAxis,
        yAxis: this.yAxis,
        series: this.series
      });
    },
  },
};
</script>
